<template>
  <a-card :bordered="false">
    <a-card title="基础配置" size="small" style="margin-bottom:15px;">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="24">
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="成为会员条件：">
                <a-radio-group v-model="handCondition.configInfo.condition">
                  <a-radio :value="1">购买礼包</a-radio>
                  <a-radio :value="2">申请会员</a-radio>
                </a-radio-group>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="粉丝保护期(0为永久)">
                <a-input-number v-model="handCondition.configInfo.protectionPeriod" :min="0" :step="1" :precision="0" placeholder="请输入粉丝保护期"
                  style="width: 40%" />
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
                <a-button type="primary" @click="saveConfig(handCondition)">保存</a-button>
              </span>
            </a-col>
          </a-row>

        </a-form>
      </div>

      <!-- 操作按钮区域 -->
      <div class="table-operator">
        <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
      </div>

      <!-- table区域-begin -->
      <div>

        <a-table ref="table" size="middle" bordered rowKey="id" :columns="columns" :dataSource="dataSource" :pagination="false" :loading="loading"
          class="j-table-force-nowrap" @change="handleTableChange">

          <template slot="htmlSlot" slot-scope="text">
            <div v-html="text"></div>
          </template>
          <template slot="imgSlot" slot-scope="text">
            <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
            <img v-else :src="getImgView(text)" height="55px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;" />
          </template>
          <template slot="fileSlot" slot-scope="text">
            <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
            <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="downloadFile(text)">
              下载
            </a-button>
          </template>

          <span slot="action" slot-scope="text, record">
            <a @click="handleEdit(record)">编辑</a>

            <a-divider type="vertical" />
            <a-dropdown>
              <a class="ant-dropdown-link">更多
                <a-icon type="down" />
              </a>
              <a-menu slot="overlay">
                <a-menu-item>
                  <a @click="handleDetail(record)">详情</a>
                </a-menu-item>
                <a-menu-item>
                  <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                    <a>删除</a>
                  </a-popconfirm>
                </a-menu-item>
              </a-menu>
            </a-dropdown>
          </span>

        </a-table>
      </div>

    </a-card>

    <a-card title="升级规则配置" size="small">
      <div class="table-page-search-wrapper">
        <div style="display:flex;line-height:42px;">
          <div style="flex:1;text-align:center;">等级</div>
          <div style="flex:2;text-align:center;">等级名称</div>
          <div style="flex:4;text-align:center;">升级规则</div>
          <div style="flex:1;text-align:center;">操作</div>
        </div>
        <div style="display:flex;line-height:42px;" v-for="(item,index) in upgradeRules.configInfo" :key="index">
          <div style="flex:1;text-align:center;">{{index+1}}</div>
          <div style="flex:2;text-align:center;">{{dataSource.length>=(index+1)?dataSource[index].name:''}}</div>
          <div style="flex:4;text-align:center;line-height:50px;">
            <div v-if="upgradeRules.configInfo[index].level==1">系统默认成为分销员后即是该等级</div>
            <div v-else>
              <div v-for="(item1,index1) in upgradeRules.configInfo[index].rules" :key="index1">
                <div v-if="upgradeRules.configInfo[index].rules[index1].type==1" style="display:flex;align-items: center;">
                  <a-switch size="small" v-model="upgradeRules.configInfo[index].rules[index1].sw" />
                  <span style="margin:0 5px;">累计订单数：自买/售卖的订单累计满</span>
                  <a-input-number size="small" v-model="upgradeRules.configInfo[index].rules[index1].limit" :min="0" :step="1" :precision="0" />
                  <span style="margin:0 5px;">单</span>
                </div>
                <div v-if="upgradeRules.configInfo[index].rules[index1].type==2" style="display:flex;align-items: center;">
                  <a-switch size="small" v-model="upgradeRules.configInfo[index].rules[index1].sw" />
                  <span style="margin:0 5px;">销售佣金：该分销员实际销售佣金满</span>
                  <a-input-number size="small" v-model="upgradeRules.configInfo[index].rules[index1].limit" :min="0" :step="1" :precision="2" />
                  <span style="margin:0 5px;">元</span>
                </div>
                <div v-if="upgradeRules.configInfo[index].rules[index1].type==3" style="display:flex;align-items: center;">
                  <a-switch size="small" v-model="upgradeRules.configInfo[index].rules[index1].sw" />
                  <a-select size="small" style="margin:0 5px;" v-model="upgradeRules.configInfo[index].rules[index1].traceType">
                    <a-select-option :value="1">
                      直属下级
                    </a-select-option>
                    <a-select-option :value="2">
                      团队下级
                    </a-select-option>
                  </a-select>
                  <a-select size="small" style="margin:0 5px;" v-model="upgradeRules.configInfo[index].rules[index1].lowerLevel">
                    <a-select-option v-for="(item3,index3) in dataSource" :key="index3" :value="item3.level">
                      {{item3.name}}
                    </a-select-option>
                  </a-select>
                  <span style="margin:0 5px;">实际消费满</span>
                  <a-input-number size="small" v-model="upgradeRules.configInfo[index].rules[index1].miniSale" :min="0" :step="1" :precision="2" />
                  <span style="margin:0 5px;">元的用户达到</span>
                  <a-input-number size="small" v-model="upgradeRules.configInfo[index].rules[index1].limit" :min="0" :step="1" :precision="0" />
                  <span style="margin:0 5px;">人</span>
                  <a-button size="small" style="margin:0 5px;" v-if="index1>2" type="danger" @click="deleteItem(index,index1)">删除
                  </a-button>
                  <a-button size="small" style="margin:0 5px;" v-if="index1==(upgradeRules.configInfo[index].rules.length-1)" type="primary"
                    @click="addItem(index)">添加
                  </a-button>
                </div>
              </div>
            </div>
          </div>
          <div style="flex:1;text-align:center;">
            <a-button v-if="index>0" type="danger" size="small" @click="clearItem(index)">清空</a-button>
          </div>
        </div>
        <a-row :gutter="24">
          <a-col :xl="24" :lg="24" :md="24" :sm="24" style="text-align:center;padding-top:20px;">
            <a-button type="primary" @click="saveConfig(upgradeRules)">保存</a-button>
          </a-col>
        </a-row>
      </div>
    </a-card>

    <hms-hand-config-modal ref="modalForm" @ok="modalFormOk"></hms-hand-config-modal>
  </a-card>
</template>

<script>
import { postAction, getAction } from '@/api/manage'
import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/MyJeecgListMixin'
import HmsHandConfigModal from './modules/HmsHandConfigModal'

export default {
  name: 'HmsHandConfigList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    HmsHandConfigModal
  },
  data () {
    return {
      description: '分销员等级管理页面',
      handCondition: {
        type: 'handCondition',//config表对应类型
        value_type: 1,//config表对应配置项数据类型1：json,2:字符串值
        configInfo: {}//config表对应配置项数据
      },
      upgradeRules: {
        type: 'upgradeRules',//config表对应类型
        value_type: 1,//config表对应配置项数据类型1：json,2:字符串值
        configInfo: []//config表对应配置项数据
      },
      isorter: {
        column: 'level',
        order: 'asc',
      },
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: "center",
          customRender: function (t, r, index) {
            return parseInt(index) + 1
          }
        },
        {
          title: '等级名称',
          align: "center",
          dataIndex: 'name'
        },
        {
          title: '等级',
          align: "center",
          dataIndex: 'level'
        },
        {
          title: '提现手续费',
          align: "center",
          dataIndex: 'feeRate'
        },
        {
          title: '创建时间',
          align: "center",
          dataIndex: 'createTime',
          customRender: function (text) {
            return !text ? "" : (text.length > 10 ? text.substr(0, 10) : text)
          }
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: "center",
          width: 147,
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        list: "/mallPlus/hmsHandConfig/list",
        delete: "/mallPlus/hmsHandConfig/delete",
        deleteBatch: "/mallPlus/hmsHandConfig/deleteBatch",
        exportXlsUrl: "/mallPlus/hmsHandConfig/exportXls",
        importExcelUrl: "mallPlus/hmsHandConfig/importExcel",

      },
      dictOptions: {},
    }
  },
  created () {
    this.getConfig('handCondition')
    this.getConfig('upgradeRules')
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },
  },
  methods: {
    clearItem (index) {
      for (let i = 0; i < this.upgradeRules.configInfo[index].rules.length; i++) {
        this.upgradeRules.configInfo[index].rules[i].sw = false
        this.upgradeRules.configInfo[index].rules[i].limit = 0
        this.upgradeRules.configInfo[index].rules[i].miniSale = 0
        this.upgradeRules.configInfo[index].rules[i].traceType = 1
        this.upgradeRules.configInfo[index].rules[i].lowerLevel = 1
      }
    },
    deleteItem (index, index1) {
      this.upgradeRules.configInfo[index].rules.splice(index1, 1)
    },
    addItem (index) {
      this.upgradeRules.configInfo[index].rules.push({
        sw: false,
        type: 3,
        title: "推广",
        traceType: 1,
        lowerTevel: 1,
        miniSale: 0,
        limit: 0
      })
    },
    handleAdd: function () {
      if (this.dataSource && this.dataSource.length > 0) {
        this.$refs.modalForm.add(this.dataSource.length + 1)
        this.$refs.modalForm.title = "新增"
        this.$refs.modalForm.disableSubmit = false
      } else {
        this.$refs.modalForm.add(1)
        this.$refs.modalForm.title = "新增"
        this.$refs.modalForm.disableSubmit = false
      }

    },
    saveConfig (configObj) {
      let that = this
      let formData = {
        type: configObj.type,
        configInfo: JSON.stringify(configObj.configInfo)
      }
      postAction("/cmsConfig/setConfigByType", formData).then((res) => {
        if (res.success) {
          that.$message.success(res.message)
        } else {
          that.$message.warning(res.message)
        }
      })
    },
    getConfig (type) {
      let that = this
      getAction("/cmsConfig/queryConfigByType", { type: type }).then((res) => {
        if (res.success) {
          if (that[type].value_type == 1) {
            that[type].configInfo = JSON.parse(res.result)
          } else if (that[type].value_type == 2) {
            that[type].configInfo = res.result
          }
          if (type == 'upgradeRules') {
            that.loadData()
          }
        } else {
          that.$message.warning(res.message)
        }
      })
    },
    loadData (arg) {
      if (!this.url.list) {
        this.$message.error("请设置url.list属性!")
        return
      }
      //加载数据 若传入参数1则加载第一页的内容
      if (arg === 1) {
        this.ipagination.current = 1
      }
      var params = this.getQueryParams()//查询条件
      getAction(this.url.list, params).then((res) => {
        if (res.success) {
          this.dataSource = res.result.records
          this.initData()
        }
      })
    },
    initData () {
      let initData = []
      for (let i = 0; i < this.dataSource.length; i++) {
        if (i == 0) {
          let initItem = {
            level: i + 1,
            rules: []
          }
          initData.push(initItem)
        } else {
          if (this.upgradeRules.configInfo.length > i) {
            let initItem = {
              level: i + 1,
              rules: [
                {
                  sw: this.upgradeRules.configInfo[i].rules[0].sw,
                  type: 1,
                  title: "累计订单数",
                  limit: this.upgradeRules.configInfo[i].rules[0].limit
                },
                {
                  sw: this.upgradeRules.configInfo[i].rules[1].sw,
                  type: 2,
                  title: "销售佣金",
                  limit: this.upgradeRules.configInfo[i].rules[1].limit
                },
                {
                  sw: this.upgradeRules.configInfo[i].rules[2].sw,
                  type: 3,
                  title: "推广",
                  traceType: this.upgradeRules.configInfo[i].rules[2].traceType,
                  lowerLevel: this.upgradeRules.configInfo[i].rules[2].lowerLevel,
                  miniSale: this.upgradeRules.configInfo[i].rules[2].miniSale,
                  limit: this.upgradeRules.configInfo[i].rules[2].limit
                }
              ]
            }
            for (let j = 3; j < this.upgradeRules.configInfo[i].rules.length; j++) {
              initItem.add({
                sw: this.upgradeRules.configInfo[i].rules[j].sw,
                type: 3,
                title: "推广",
                traceType: this.upgradeRules.configInfo[i].rules[j].traceType,
                lowerLevel: this.upgradeRules.configInfo[i].rules[j].lowerLevel,
                miniSale: this.upgradeRules.configInfo[i].rules[j].miniSale,
                limit: this.upgradeRules.configInfo[i].rules[j].limit
              })
            }

            initData.push(initItem)
          } else {
            let initItem = {
              level: i + 1,
              rules: [
                {
                  sw: false,
                  type: 1,
                  title: "累计订单数",
                  limit: 0
                },
                {
                  sw: false,
                  type: 2,
                  title: "销售佣金",
                  limit: 0
                },
                {
                  sw: false,
                  type: 3,
                  title: "推广",
                  traceType: 1,
                  lowerLevel: 1,
                  miniSale: 1,
                  limit: 1
                }
              ]
            }
            initData.push(initItem)
          }
        }


      }
      this.upgradeRules.configInfo = initData

    },
    initDictConfig () {
    }
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>
